package com.yc.droid;

import android.os.Environment;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;
import java.util.Locale;

/**
 * Created by Administrator on 2016/4/27 0027.
 */
public class DataFile {
    public static String file_name;
    private static FileOutputStream outStream;
    private static OutputStreamWriter writer;

    public static void file_end() {
        try {
            DataFile.writer.flush();
            DataFile.writer.close();
            DataFile.outStream.close();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        new DataFinally(DataFile.file_name);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
        catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public static void file_new() {
        DataFile.file_name = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss_E", Locale.getDefault()).format(System.currentTimeMillis());
        if (DataFile.file_name == null) {
            return;
        }
        try {
            final File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/MotionDetect");
            file.mkdirs();
            DataFile.outStream = new FileOutputStream(new File(file, DataFile.file_name + ".txt"), true);
            DataFile.writer = new OutputStreamWriter(DataFile.outStream, "utf-8");
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public static void file_write(final String s) {
        try {
            DataFile.writer.write(s + "\n");
        }
        catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}
